package com.jysz.certificate.service.cs;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.persistence.dao.cs.tn.TheNetworkDao;
import com.jysz.certificate.persistence.enums.AttachmentType;
import com.jysz.certificate.persistence.enums.WorkflowType;
import com.jysz.certificate.persistence.enums.cs.TheNetWorkStatus;
import com.jysz.certificate.persistence.model.cs.TheNetwork;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.service.core.AttachmentService;
import com.jysz.certificate.service.workflow.WorkflowService;
import com.jysz.certificate.utils.DateUtil;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

@Service
public class TheNetworkService  extends EntityService<TheNetwork, Long, TheNetworkDao>{
	
	@Resource
	private TheNetworkDao theNetworkDao;
	@Resource
	private AttachmentService attService;
	@Resource
	private WorkflowService workflowService;
	@Override
	public TheNetworkDao getEntityDao() {
		return theNetworkDao;
	}
	public Page<TheNetwork> selectPage(TheNetwork theNetwork,int pageNo, int pageSize,Date startDate ,Date endDate,Integer state,User user,User manager){
		Criteria criteria = theNetworkDao.createCriteria();
		criteria.addOrder(Order.desc("updatedAt"));
		Criteria shopCriteria = criteria.createCriteria("shop");
		if(theNetwork!=null){
			if(theNetwork.getShop()!=null && StringUtils.isNotBlank(theNetwork.getShop().getShopMark())){
				shopCriteria.add(Restrictions.eq("shopMark", theNetwork.getShop().getShopMark()));
			}
			if(theNetwork.getBrand()!=null && StringUtils.isNotBlank(theNetwork.getBrand().getName())){
				criteria.createCriteria(TheNetwork.COLUMN_BRAND).add(Restrictions.eq("name", theNetwork.getBrand().getName()));
			}
			if(theNetwork.getFinancial()!=null && StringUtils.isNotBlank(theNetwork.getFinancial().getName())){
				criteria.createCriteria(TheNetwork.COLUMN_FINANCIAL).add(Restrictions.eq("name", theNetwork.getFinancial().getName()));
			}
			
		}
		if(startDate !=null || null !=endDate){
			if(startDate !=null ){
				criteria.add(Restrictions.gt(TheNetwork.COLUMN_APPLICATIONDATE, startDate));
			}
			if(endDate !=null ){
				criteria.add(Restrictions.le(TheNetwork.COLUMN_APPLICATIONDATE,  DateUtil.addDays(endDate,1)));
			}
			if(startDate !=null && null !=endDate){
				criteria.add(Restrictions.between(TheNetwork.COLUMN_APPLICATIONDATE, startDate, endDate));
			}
		}
		if(state!=null && state>=0){
			criteria.add(Restrictions.eq("state", TheNetWorkStatus.index(state)));
		}
		
		if(user!=null){
			Long ary[]=theNetworkDao.getShops(user);
			if(ary!=null){
				shopCriteria.add(Restrictions.in("id",ary));
			}else {
				return null;
			}
		}
		if(manager!=null){
			Long ary[]=theNetworkDao.getShops(manager);
			if(ary!=null){
				
				shopCriteria.add(Restrictions.in("id",ary));
			}else {
				return null;
			}
		}
		theNetworkDao.addShopCriteria(shopCriteria, true, null);
		return this.findPage(criteria,pageNo,pageSize);
	}
	public void save(TheNetwork the,File file,String fileName,User user){
		super.save(the);
		if(file!=null)
			attService.addFile(user, AttachmentType.THEINTERNET, file, the.getId(),fileName);
	}
	/**
	 * 二网审核数量
	 * @param shops
	 * @return
	 */
	public Integer waitTheNetWork(Set<Shop> shops){
		Criteria criteria = theNetworkDao.createCriteria();
		criteria.setProjection(Projections.rowCount());
		
		criteria.add(Restrictions.in("shop", shops));
		criteria.add(Restrictions.eq("state", TheNetWorkStatus.SUBMITED));
		
		return ((Number)criteria.uniqueResult()).intValue();
	}
	
	public Long[] cerIds(Long[] ids){
		return theNetworkDao.cerIds(ids);
	}
	
	public void destoryAndWorkflow(Long id) {
		workflowService.destory(id, WorkflowType.SECOND);
		attService.delFile(id, AttachmentType.THEINTERNET);
		this.destory(id);
	}

}
